package com.ofek.applicationbase.forms;

import java.io.Serializable;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import com.ofek.applicationbase.broadcast.Delegate;
import com.ofek.applicationbase.broadcast.DelegateBroadcaster;
import com.ofek.applicationbase.utils.Log;

public abstract class QuestionView extends RelativeLayout {
	private boolean required=true;
	private final DelegateBroadcaster<Object> answerChangedListener = new DelegateBroadcaster<Object>();
	public QuestionView(final int resourceId,final Context context) {
		super(context);
		init(resourceId);
		setFocusable(false);
	}
	public QuestionView(final int resourceId,final Context context,final boolean required) {
		super(context);
		init(resourceId);
		this.required=required;
	}
	private void init(final int resourceId) {
		final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(resourceId, this);

	}

	public abstract Serializable getAnswer();

	public abstract void setAnswer(Object defaultAnswer);

	/**
	 * 
	 * @param defaultAnswer - the default answer
	 * @param shouldPrePick - should it be picked when nothing has been picked yet
	 */
	public abstract void setDefaultAnswer(Object defaultAnswer,boolean shouldPrePick);


	public void setRequired(final boolean required) {
		this.required=required;
	}

	public boolean isRequired() {
		return required;
	}
	public abstract void setEditable(boolean edit);

	public boolean registerAnswerChangedListener(final Delegate<Object> listener) {
		return answerChangedListener.register(listener);
	}

	public boolean unregisterAnswerChangedListener(final Delegate<Object> listener) {
		return answerChangedListener.unregister(listener);
	}

	protected void broadcastAnswerChanged(final Object newAnswer) {
		Log.d("broadcastQuestionChanged" + newAnswer);
		answerChangedListener.broadcast(newAnswer);
	}
	public void animateInvalidInput() {
	}
	public boolean hasValidInput() {
		return !isRequired() || getAnswer() != null;
	}

}
